import {messages} from "share/common";
/**
 * 操作：去报销
 * 适用：关联报销单的申请单
 */
import React from 'react'
import { connect } from 'react-redux'
import menuRoute from 'routes/menuRoute'
import { Form, Button, Modal, Select, message, Row, Col, Alert } from 'antd'
const FormItem = Form.Item;
const Option = Select.Option;
import requestService from 'containers/request/request.service'

class GoErBtn extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      info: {},
      erFormOID: '', //报销单formOID
    };
    this.newExpenseReport = menuRoute.getRouteItem('new-expense-report','key'); //新建报销单页
  }

  componentWillMount(){}

  componentWillReceiveProps(nextProps) {
    if (!this.state.info.applicationOID) {
      this.setState({
        info: nextProps.info
      });
    }
  }

  //校验是否可以创建报销单
  checkIsGoErBtn = () => {
    const { info } = this.state;
    this.setState({loading: true});
    requestService.checkExpenseReport(info.expenseReportApplicantOID, info.applicationOID).then(res => {
      if (res.data) {
        if (res.data.success) {
          this.setState({erFormOID: res.data.rows && res.data.rows.expenseReportFormOID}, this.goCreateEr);
        } else {
          message.error(res.data.message);
          this.setState({loading: false});
        }
      }
    }).catch(err =>{
      this.setState({loading: false});
      if (err.response && err.response.data) {
        message.error(err.response.data.message);
      }
    });
  };

  goCreateEr = () => {
    const { info, erFormOID } = this.state;
    let url = this.newExpenseReport.url;
    url = url.replace(':formId', erFormOID).replace(':userOID', info.expenseReportApplicantOID).replace(':applicationOID', info.applicationOID);
    if (info.jobId) {
      url = url.replace(':applicantJobId', info.jobId);
    }
    this.context.router.push(url);
  };

  render() {
    const { info, loading } = this.state;
    return (
      <div className="request-btn">
        {info.expenseReportButtonDisplay && (
          <Button type="primary" loading={loading} onClick={this.checkIsGoErBtn}>{messages('request.key605')/*去报销*/}</Button>
        )}
      </div>
    )
  }
}

GoErBtn.propTypes = {
  info: React.PropTypes.object.isRequired
};

GoErBtn.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps(state) {
  return {}
}

export default connect(mapStateToProps)(GoErBtn)
